package com.bazaaroid.client.web.gwt.partnermodule.client.view.products.impl;

import java.util.HashMap;
import java.util.Map;

import com.bazaaroid.client.web.gwt.partnermodule.client.dto.ProductDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.event.products.handler.ProductViewHandler;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.LoadingPanel;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.TitleCommandBar;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.products.EditProductEntryView;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.products.ProductEntryView;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.products.ProductsTabView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ProductsTabViewImpl extends Composite implements ProductsTabView {

	@UiTemplate("ProductsTabView.ui.xml")
	interface ProductsTabViewUiBinder extends
			UiBinder<Widget, ProductsTabViewImpl> {
	}

	private static ProductsTabViewUiBinder uiBinder = GWT
			.create(ProductsTabViewUiBinder.class);

	private Presenter presenter;
	
	private LoadingPanel loadingPanel = new LoadingPanel(new Label("Loading"));
	private LoadingPanel savingPanel = new LoadingPanel(new Label("Loading"));
	private Map<ProductDTO, Widget> productViews = new HashMap<ProductDTO, Widget>();
	private ProductViewHandler productViewHandler;
	
	@UiField VerticalPanel productListPanel;
	
	private class NewProductClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			ProductDTO productDTO = new ProductDTO();
			EditProductEntryView editProductEntryView = new EditProductEntryView(ProductsTabViewImpl.this, productDTO);
			productListPanel.insert(editProductEntryView, 0);
			productViews.put(productDTO, editProductEntryView);
		}
	}
	
	public ProductsTabViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		productListPanel.add(loadingPanel);
		productListPanel.add(savingPanel);
		
		TitleCommandBar titleBar = new TitleCommandBar("Products Management");
		titleBar.addCommand("New Product", new NewProductClickHandler());
		productListPanel.add(titleBar);
		
		ProductDTO productDTO = new ProductDTO();
		productDTO.setName("Test Product Name");
		productDTO.setDescription("bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla");
		addEntryAtEnd(productDTO);
		ProductDTO productDTO2 = new ProductDTO();
		productDTO2.setName("Test Product Name 2");
		productDTO2.setDescription("bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla");
		addEntryAtEnd(productDTO2);
	}
	
	public ProductViewHandler getProductViewHandler() {
		return productViewHandler;
	}

	public LoadingPanel getLoadingPanel() {
		return loadingPanel;
	}
	
	public LoadingPanel getSavingPanel() {
		return savingPanel;
	}
	
	public void addEntryAtStart(ProductDTO productDTO) {
		ProductEntryView productEntryView = new ProductEntryView(this, productDTO);
		productListPanel.insert(productEntryView, 0);
		productViews.put(productDTO, productEntryView);
	}
	
	public void addEntryAtEnd(ProductDTO productDTO) {
		ProductEntryView productEntryView = new ProductEntryView(this, productDTO);
		productListPanel.add(productEntryView);
		productViews.put(productDTO, productEntryView);
	}
	
	public void startEdit(ProductEntryView productEntryView) {
		EditProductEntryView editProductEntryView = new EditProductEntryView(productEntryView);
		swapViews(productEntryView, editProductEntryView, productEntryView.getProductDTO());
	}
	
	public void endEdit(EditProductEntryView editProductEntryView, ProductEntryView productEntryView) {
		swapViews(editProductEntryView, productEntryView, productEntryView.getProductDTO());
	}
	
	public void onProductEntrySaved(ProductEntryView productEntryView, EditProductEntryView editProductEntryView, ProductDTO productDTO) {
		productViewHandler.onProductEntrySaved(productDTO);
		if( productEntryView != null ) {
			productEntryView.update();
			productEntryView.getProductsTabView().endEdit(editProductEntryView, productEntryView);
		} else {
			addEntryAtStart(productDTO);
			editProductEntryView.removeFromParent();
		}
	}
	
	public void swapViews(Widget oldView, Widget newView, ProductDTO productDTO) {
		int index = productListPanel.getWidgetIndex(oldView);
		oldView.removeFromParent();
		productListPanel.insert(newView, index);
		productViews.put(productDTO, newView);
	}
	
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	public Widget asWidget() {
		return this;
	}

}
